package com.example.edupublisher.controller;

import com.example.edupublisher.bean.ExamDurationStats;
import com.example.edupublisher.bean.ExamStats;
import com.example.edupublisher.service.ExamService;
import com.example.edupublisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ExamController {

    @Autowired
    private ExamService examService;

    @RequestMapping("/ExamStats")
    public String getCourseReview(@RequestParam(value = "date", defaultValue = "1") Integer date){
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<ExamStats> courseExamStats = examService.getCourseExamStats(date);
        if (courseExamStats == null || courseExamStats.size() == 0) {
            return "";
        }
        StringBuilder rows = new StringBuilder("[");

        for (int i = 0; i < courseExamStats.size(); i++) {
            ExamStats examStats = courseExamStats.get(i);
            String examCount = examStats.getExamCount();
            Double avgScore = examStats.getAvgScore();
            Integer avgTime = examStats.getAvgTime();
            rows.append("{\n" +
                    "\"examCount\": \""+ examCount +"\",\n" +
                    "\"avgScore\": \""+ avgScore +"\",\n" +
                    "\"avgTime\": \""+ avgTime +"\"\n" +
                    "}");
            if (i < courseExamStats.size() - 1){
                rows.append(",");
            }else {
                rows.append("]");
            }
        }
        String str = "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"考试人次\",\n" +
                "        \"id\": \"examCount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均分\",\n" +
                "        \"id\": \"avgScore\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"平均时长\",\n" +
                "        \"id\": \"avgTime\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"superHeaders\": [\n" +
                "      [\n" +
                "        {\n" +
                "          \"name\": \"考试域各课程考试统计\",\n" +
                "          \"colspan\": 3\n" +
                "        }\n" +
                "      ]\n" +
                "    ],\n" +
                "\"rows\" : "  + rows + "\n" +
                "}\n" +
                "}";

        String finalStr = str.replaceAll("\\\\", ",");
        return finalStr;
    }


    @RequestMapping("/ExamDuration")
    public  String getExamDuration(@RequestParam(value = "date", defaultValue = "1") Integer date){
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<ExamDurationStats> examDurationStats = examService.getExamDurationStats(date);
        if (examDurationStats == null || examDurationStats.size() == 0) {
            return "";
        }
        StringBuilder row1 = new StringBuilder("");
        StringBuilder row2 = new StringBuilder("");

        for (int i = 0; i < examDurationStats.size(); i++) {
            ExamDurationStats durationStats = examDurationStats.get(i);
            String scoreDuration = durationStats.getScoreDuration();
            Long userCount = durationStats.getUserCount();

            row1.append("\""+ scoreDuration +"\"");
            row2.append("\""+ userCount +"\"");
            if (i < examDurationStats.size() - 1){
                row1.append(",");
                row2.append(",");
            }else {
                row1.append("");
                row2.append("");
            }
        }

        String str = "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n" + row1 +
                "    ],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"各试卷成绩分布\",\n" +
                "        \"data\": [\n" + row2 +
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
        return str.replaceAll("\\\\", ",");
    }

}
